package com.megaspacevr.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码测试工具类
 */
public class PasswordTestUtil {
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = "admin123";
        String dbPassword = "$2a$10$7JB720yubVSeLan6Zzg2XO8W1l0wJlS2Q2HHMb4ZLWt7Uef.Lsc.i";
        
        System.out.println("=== BCrypt密码测试 ===");
        System.out.println("原始密码: " + rawPassword);
        System.out.println("数据库密码: " + dbPassword);
        
        // 验证数据库中的密码
        boolean dbMatches = encoder.matches(rawPassword, dbPassword);
        System.out.println("数据库密码验证结果: " + dbMatches);
        
        // 生成新的加密密码
        System.out.println("\n=== 生成新的BCrypt密码 ===");
        for (int i = 1; i <= 5; i++) {
            String newEncoded = encoder.encode(rawPassword);
            boolean newMatches = encoder.matches(rawPassword, newEncoded);
            System.out.println("密码" + i + ": " + newEncoded);
            System.out.println("验证" + i + ": " + newMatches);
            System.out.println();
        }
        
        // 分析数据库密码格式
        System.out.println("=== 密码格式分析 ===");
        System.out.println("数据库密码长度: " + dbPassword.length());
        System.out.println("是否以$2a$开头: " + dbPassword.startsWith("$2a$"));
        System.out.println("密码格式正确: " + (dbPassword.matches("\\$2[aby]?\\$\\d{1,2}\\$.{53}")));
        
        // 手动测试不同的密码
        String[] testPasswords = {"admin123", "Admin123", "ADMIN123", "admin"};
        System.out.println("\n=== 测试不同密码 ===");
        for (String testPwd : testPasswords) {
            boolean matches = encoder.matches(testPwd, dbPassword);
            System.out.println("密码 '" + testPwd + "' 验证结果: " + matches);
        }
    }
} 